package cn.wolfcode.luowowo.article.domain;

import cn.wolfcode.luowowo.common.domain.BaseDomain;
import cn.wolfcode.luowowo.common.util.TimeDifference;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Setter
@Getter
public class Flight extends BaseDomain {

    private String departCityName; //出发城市

    private String destCityName;  //到达城市

    private String airlines;    //航空公司

    private String aircraftModels;  //机型

    private String flightNum;  //航班号

    private String departureTime;   //出发时间

    private String landingTime;     //到达时间

    private String departureAirport;    //出发机场

    private String landingAirport;  //到达机场

    private Boolean is_mon; //是否是星期一

    private Boolean is_tue; //是否是星期二

    private Boolean is_wed; //是否是星期三

    private Boolean is_thr; //是否是星期四

    private Boolean is_fri; //是否是星期五

    private Boolean is_sat; //是否是星期六

    private Boolean is_sun; //是否是星期天

    private String logoUrl; //航空公司logo

    private String betweenTime;  //飞行时间

    private int price;  //价格

    public int getPrice() {
        Random random = new Random();
        int i = random.nextInt(200);
        int num = 900+i;
        return num;
    }

    //把时间的秒数去掉
    public String getDepartureTime() {
        String substring = departureTime.substring(0, 5);
        return substring;
    }
    public String getLandingTime() {
        String substring = landingTime.substring(0, 5);
        return substring;
    }

    //获取飞行时间
    public String getBetweenTime() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date1 = sdf.parse(landingTime);
        Date date2 = sdf.parse(departureTime);
        String s = TimeDifference.getTimeDifference(date2, date1);
        String[] split = s.split(":");
        StringBuilder builder = new StringBuilder();
        if(split.length==3){
            builder.append(split[0]).append("小时").append(split[1]).append("分钟");
        }else if(split.length==2){
            builder.append(split[0]).append("分钟");
        }
        return builder.toString();
    }

    public String getJsonString() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);

        map.put("airlines", airlines);

        map.put("departureAirport", departureAirport);

        map.put("departureTime", departureTime);

        map.put("aircraftModels", aircraftModels);

        return JSON.toJSONString(map);
    }
}